package com.ezia.coreservices.charging.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ezia.coreservices.charging.service.MaOrderService;
import com.ezia.coreservices.charging.utils.ResultMsg;
import com.ezia.coreservices.charging.utils.ResultStatusCode;


/**
 * 订单类
 * 
 * add by yc
 */
@RestController
public class MaOrderController{
	
	private static final Logger logger = LoggerFactory.getLogger(MaOrderController.class);
	
	@Autowired
	private MaOrderService maorderservice;

	/**获取充电订单信息 */
	@RequestMapping(value = "/{version}/charging/vehicle/external/charge/bill", method = RequestMethod.POST,
			produces="application/json;charset=UTF-8")
	public Object getChargeBill(@PathVariable("version") String version,String orderId,@RequestBody Map<Object,String> userInfo) {
		if(orderId==null) {
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Parameter_Error.getRespCode(), ResultStatusCode.Parameter_Error.getRespMsg(),
					null);
			return resultMsg;
		}
		try {
			Object Result = maorderservice.getChargeBill(orderId);
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(),
					Result);
			return resultMsg;
		} catch (Exception e) {
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Server_Error.getRespCode(), ResultStatusCode.Server_Error.getRespMsg(),
					null);
			return resultMsg;
		}
	
	}
	
	/**订单查询接口 */
	@RequestMapping(value = "/{version}/charging/vehicle/external/order/ordersearch", method = RequestMethod.POST,
			produces="application/json;charset=UTF-8")
	public Object getOrderInfo(@PathVariable("version") String version,String orderId,@RequestBody Map<Object,String> userInfo) {
		if(orderId==null) {
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Parameter_Error.getRespCode(), ResultStatusCode.Parameter_Error.getRespMsg(),
					null);
			return resultMsg;
		}
		try {
			Object Result = maorderservice.getOrderInfo(orderId);
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(),
					Result);
			return resultMsg;
		} catch (Exception e) {
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Server_Error.getRespCode(), ResultStatusCode.Server_Error.getRespMsg(),
					null);
			return resultMsg;
		}
	
	}
	
	@RequestMapping(value = "/{version}/charging/vehicle/external/order/orderhistorysearch", method = RequestMethod.POST,
			produces="application/json;charset=UTF-8")
	public Object getOrderHistoryInfo(@PathVariable("version") String version,
			@RequestParam(value = "StartTime", required = false)String StartTime,
			@RequestParam(value = "EndTime", required = false)String EndTime,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestBody Map<Object,String> userInfo) {
		
		if(StartTime==null||EndTime==null) {
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Parameter_Error.getRespCode(), ResultStatusCode.Parameter_Error.getRespMsg(),
					null);
			return resultMsg;
		}
		int offset = 0;
		if(String.valueOf(pageSize)=="null"&&String.valueOf(pageSize)=="null") {
			pageSize = 10;
			pageNo = 1;
			offset = (pageNo - 1) * pageSize;
		}else {
			offset = (pageNo - 1) * pageSize;
		}
		try {
			Object Result = maorderservice.getOrderHistoryInfo(StartTime, EndTime, userInfo.get("idValue"), offset, pageSize, pageNo);
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(),
					Result);
			return resultMsg;
		} catch (Exception e) {
			ResultMsg resultMsg = new ResultMsg(ResultStatusCode.Server_Error.getRespCode(), ResultStatusCode.Server_Error.getRespMsg(),
					null);
			return resultMsg;
		}
		
	}

}
